package server

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"sync"

	"go.uber.org/zap"
)

// Grafana ...
type Grafana struct {
	sync.RWMutex
	apis          map[int]*apiGrafana
	addr          string
	authorization string
	accept        string
	contentType   string
	templateJSON  string
}

// NewGrafana ...
func NewGrafana() *Grafana {
	return &Grafana{
		apis: make(map[int]*apiGrafana),
	}
}

func (grafana *Grafana) addAPI(api *apiGrafana) {
	grafana.RLock()
	_, ok := grafana.apis[api.apiType]
	grafana.Unlock()
	if ok {
		return
	}
	grafana.Lock()
	grafana.apis[api.apiType] = api
	grafana.Unlock()
}

// Start ...
func (grafana *Grafana) Start() error {
	if err := grafana.initGrafana(); err != nil {
		Logger.Error("grafana", zap.Error(err))
		return err
	}
	return nil
}

func (grafana *Grafana) loadApis() error {
	// query := fmt.Sprintf("select `type`, `api_key`, `key_value`, `post_body`, `describe` from  grafana_api;")
	// rows, err := gServer.sql.DB.Query(query)
	// if err != nil {
	// 	Logger.Fatal("load from mysql error", zap.Error(err))
	// }

	// for rows.Next() {
	// 	var apiType int
	// 	var keyValueStr sql.NullString
	// 	var apiKey, postBody, describe sql.NullString
	// 	err := rows.Scan(&apiType, &apiKey, &keyValueStr, &postBody, &describe)
	// 	if err != nil {
	// 		Logger.Error("Scan", zap.Error(err))
	// 		continue
	// 	}
	// 	api := newapiGrafana()
	// 	api.apiType = apiType
	// 	api.apiKey = apiKey.String
	// 	// 加载interface

	// 	grafana.addAPI(api)
	// }
	return nil
}

func (grafana *Grafana) initGrafana() error {
	query := fmt.Sprintf("select `addr`, `authorization`, `accept`, `content_type`, `template_json` from  grafana;")
	rows, err := gServer.sql.DB.Query(query)
	if err != nil {
		Logger.Warn("load from mysql error", zap.Error(err))
		return nil
	}
	defer rows.Close()

	for rows.Next() {
		err := rows.Scan(&grafana.addr, &grafana.authorization, &grafana.accept, &grafana.contentType, &grafana.templateJSON)
		if err != nil {
			Logger.Error("Scan", zap.Error(err))
			continue
		}
		break
	}
	return nil
}

func (grafana *Grafana) insertApp(cr *CreateResp) error {
	query := fmt.Sprintf("insert into `app` ( `name`, `app_id`, `slug`, `status`, `uid`, `url`, `version`) values ( '%s', '%d', '%s', '%s', '%s', '%s', '%d')",
		cr.Name, cr.ID, cr.Slug, cr.Status, cr.UID, cr.URL, cr.Version)
	log.Println(query)
	rows, err := gServer.sql.DB.Query(query)
	if err != nil {
		Logger.Warn("insertDebugResult", zap.Error(err))
		return err
	}
	rows.Close()
	return nil
}

// CreateResp ...
type CreateResp struct {
	Name    string
	ID      int    `json:"id"`
	Slug    string `json:"slug"`
	Status  string `json:"status"`
	UID     string `json:"uid"`
	URL     string `json:"url"`
	Version int    `json:"version"`
}

// CreateDashboard 新建dashboard
func (grafana *Grafana) CreateDashboard(name string) error {
	data := make(map[string]interface{})
	err := json.Unmarshal([]byte(grafana.templateJSON), &data)
	if err != nil {
		Logger.Error("json", zap.Error(err))
		return err
	}

	dv, ok := data["dashboard"]
	if !ok {
		return fmt.Errorf("unknow json: %s", grafana.templateJSON)
	}
	var body *bytes.Reader
	switch dashboard := dv.(type) {
	case map[string]interface{}:
		dashboard["tags"] = []string{"压测平台", name}
		dashboard["title"] = name
		dashboard["uid"] = nil
		bodyData, err := json.Marshal(&data)
		if err != nil {
			Logger.Error("json", zap.Error(err))
			return err
		}
		body = bytes.NewReader([]byte(strings.Replace(string(bodyData), "google.local", name, -1)))
		break
	default:
		return fmt.Errorf("unknow json: %s", grafana.templateJSON)
	}

	request, err := http.NewRequest("POST", grafana.addr+"/api/dashboards/db", body)
	if err != nil {
		Logger.Error("NewRequest", zap.Error(err))
		return err
	}
	request.Header.Set("Authorization", grafana.authorization)
	request.Header.Set("Accept", grafana.accept)
	request.Header.Set("Content-Type", grafana.contentType)

	var resp *http.Response
	resp, err = http.DefaultClient.Do(request)
	if err != nil {
		Logger.Error("json", zap.Error(err))
		return err
	}

	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		Logger.Error("json", zap.Error(err))
		return err
	}

	if resp.StatusCode == http.StatusOK {
		cr := &CreateResp{
			Name: name,
		}
		err := json.Unmarshal(b, cr)
		if err != nil {
			Logger.Error("json", zap.Error(err))
			return err
		}
		// insert db
		err = grafana.insertApp(cr)
		if err != nil {
			Logger.Error("insertApp", zap.Error(err))
			return err
		}
	}

	Logger.Info("CreateDashboard", zap.String("请求返回结果", string(b)))
	return nil
}

// CreateDashboard2222 ....
func (grafana *Grafana) CreateDashboard2222(name string) error {
	data := make(map[string]interface{})
	err := json.Unmarshal([]byte(createJson), &data)
	if err != nil {
		Logger.Error("json", zap.Error(err))
		return err
	}

	dv, ok := data["dashboard"]
	if !ok {
		return fmt.Errorf("unknow json: %s", createJson)
	}
	var body *bytes.Reader //:= bytes.NewReader(data)
	switch dashboard := dv.(type) {
	case map[string]interface{}:
		dashboard["tags"] = []string{"压测平台", "测试模版"}
		dashboard["title"] = name
		dashboard["uid"] = nil
		bodyData, err := json.Marshal(&data)
		if err != nil {
			Logger.Error("json", zap.Error(err))
			return err
		}
		// log.Println(string(bodyData))
		body = bytes.NewReader([]byte(strings.Replace(string(bodyData), "google.local", name, -1)))
		break
	default:
		return fmt.Errorf("unknow json: %s", createJson)
	}
	// log.Println(body)

	request, err := http.NewRequest("POST", "http://10.100.7.46:3000/api/dashboards/db", body)
	if err != nil {
		Logger.Error("NewRequest", zap.Error(err))
		return err
	}
	request.Header.Set("Authorization", "Bearer eyJrIjoib2tLQTYyUzB2RUhwa2Q3bXhXQU0xck10eHRld3pXeTAiLCJuIjoiaGVybWVzIiwiaWQiOjF9")
	request.Header.Set("Accept", "application/json")
	request.Header.Set("Content-Type", "application/json")

	var resp *http.Response
	resp, err = http.DefaultClient.Do(request)
	if err != nil {
		Logger.Error("json", zap.Error(err))
		return err
	}

	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		Logger.Error("json", zap.Error(err))
		return err
	}

	if resp.StatusCode == http.StatusOK {
		cr := &CreateResp{}
		err := json.Unmarshal(b, cr)
		if err != nil {
			Logger.Error("json", zap.Error(err))
			return err
		}
		// insert db
		err = grafana.insertApp(cr)
		if err != nil {
			Logger.Error("insertApp", zap.Error(err))
			return err
		}
	}

	Logger.Info("CreateDashboard", zap.String("请求返回结果", string(b)))
	return nil
}

// Getlink ...
func (grafana *Grafana) Getlink(uuid string) error {
	return nil
}

type apiGrafana struct {
	apiType  int
	apiKey   string
	value    map[string]interface{}
	postBody map[string]interface{}
	describe string
}

func newapiGrafana() *apiGrafana {
	return &apiGrafana{
		value:    make(map[string]interface{}),
		postBody: make(map[string]interface{}),
	}
}

var createJson string = `
	{
		"dashboard": {
			"annotations": {
			  "list": [
				{
				  "builtIn": 1,
				  "datasource": "-- Grafana --",
				  "enable": true,
				  "hide": true,
				  "iconColor": "rgba(0, 211, 255, 1)",
				  "name": "Annotations & Alerts",
				  "type": "dashboard"
				}
			  ]
			},
			"editable": true,
			"gnetId": null,
			"graphTooltip": 0,
			"id": 0,
			"links": [],
			"panels": [
			  {
				"collapsed": true,
				"gridPos": {
				  "h": 1,
				  "w": 24,
				  "x": 0,
				  "y": 0
				},
				"id": 36,
				"panels": [
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 6,
					  "w": 8,
					  "x": 0,
					  "y": 1
					},
					"id": 40,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "netstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"tcp_established"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "TCP Estab",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 6,
					  "w": 8,
					  "x": 8,
					  "y": 1
					},
					"id": 42,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "netstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"tcp_time_wait"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "Time Wait连接数",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 6,
					  "w": 8,
					  "x": 16,
					  "y": 1
					},
					"id": 38,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "netstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"tcp_syn_recv"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "TCP Syn Recv数",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  }
				],
				"title": "Netstat",
				"type": "row"
			  },
			  {
				"collapsed": true,
				"gridPos": {
				  "h": 1,
				  "w": 24,
				  "x": 0,
				  "y": 1
				},
				"id": 18,
				"panels": [
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 5,
					  "w": 8,
					  "x": 0,
					  "y": 2
					},
					"id": 26,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "ifstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"in_bytes"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "In速率",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "bytes",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 5,
					  "w": 8,
					  "x": 8,
					  "y": 2
					},
					"id": 24,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "ifstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"out_bytes"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "Out速率",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "bytes",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 5,
					  "w": 8,
					  "x": 16,
					  "y": 2
					},
					"id": 20,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "ifstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"in_packets"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "in packet速率",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 5,
					  "w": 8,
					  "x": 0,
					  "y": 7
					},
					"id": 28,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "ifstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"out_packets"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "out packet速率",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 5,
					  "w": 8,
					  "x": 8,
					  "y": 7
					},
					"id": 30,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "ifstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"in_errors"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "in errors / s",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 5,
					  "w": 8,
					  "x": 16,
					  "y": 7
					},
					"id": 22,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "ifstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"out_errors"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "Out errors / s",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 6,
					  "w": 8,
					  "x": 0,
					  "y": 12
					},
					"id": 32,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "ifstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"in_dropped"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "In dropped / s",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 6,
					  "w": 8,
					  "x": 8,
					  "y": 12
					},
					"id": 34,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "ifstat",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"out_dropped"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "Out dropped / s",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  }
				],
				"title": "Ifstat",
				"type": "row"
			  },
			  {
				"collapsed": true,
				"gridPos": {
				  "h": 1,
				  "w": 24,
				  "x": 0,
				  "y": 2
				},
				"id": 2,
				"panels": [
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 7,
					  "w": 8,
					  "x": 0,
					  "y": 25
					},
					"id": 6,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 2,
					"links": [],
					"nullPointMode": "connected",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "cpu",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"usage_idle"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "idle",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "percent",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 7,
					  "w": 8,
					  "x": 8,
					  "y": 25
					},
					"id": 8,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "cpu",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"usage_iowait"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "IO Wait",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "percent",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 7,
					  "w": 8,
					  "x": 16,
					  "y": 25
					},
					"id": 4,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "cpu",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"usage_user"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "User Usage",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "percent",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  }
				],
				"title": "CPU",
				"type": "row"
			  },
			  {
				"collapsed": true,
				"gridPos": {
				  "h": 1,
				  "w": 24,
				  "x": 0,
				  "y": 3
				},
				"id": 10,
				"panels": [
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 7,
					  "w": 8,
					  "x": 0,
					  "y": 33
					},
					"id": 16,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "system",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"load1"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "Load",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 7,
					  "w": 8,
					  "x": 8,
					  "y": 33
					},
					"id": 14,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "mem",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"available_percent"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "Idle",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "percent",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  },
				  {
					"aliasColors": {},
					"bars": false,
					"dashLength": 10,
					"dashes": false,
					"datasource": "influxdb",
					"fill": 1,
					"gridPos": {
					  "h": 7,
					  "w": 8,
					  "x": 16,
					  "y": 33
					},
					"id": 12,
					"legend": {
					  "avg": false,
					  "current": true,
					  "max": false,
					  "min": false,
					  "show": true,
					  "total": false,
					  "values": true
					},
					"lines": true,
					"linewidth": 1,
					"links": [],
					"nullPointMode": "null",
					"percentage": false,
					"pointradius": 5,
					"points": false,
					"renderer": "flot",
					"seriesOverrides": [],
					"spaceLength": 10,
					"stack": false,
					"steppedLine": false,
					"targets": [
					  {
						"groupBy": [
						  {
							"params": [
							  "$__interval"
							],
							"type": "time"
						  },
						  {
							"params": [
							  "null"
							],
							"type": "fill"
						  }
						],
						"measurement": "mem",
						"orderByTime": "ASC",
						"policy": "default",
						"refId": "A",
						"resultFormat": "time_series",
						"select": [
						  [
							{
							  "params": [
								"available"
							  ],
							  "type": "field"
							},
							{
							  "params": [],
							  "type": "mean"
							}
						  ]
						],
						"tags": [
						  {
							"key": "app",
							"operator": "=",
							"value": "google.local"
						  }
						]
					  }
					],
					"thresholds": [],
					"timeFrom": null,
					"timeShift": null,
					"title": "可用内存大小",
					"tooltip": {
					  "shared": true,
					  "sort": 0,
					  "value_type": "individual"
					},
					"type": "graph",
					"xaxis": {
					  "buckets": null,
					  "mode": "time",
					  "name": null,
					  "show": true,
					  "values": []
					},
					"yaxes": [
					  {
						"format": "bytes",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  },
					  {
						"format": "short",
						"label": null,
						"logBase": 1,
						"max": null,
						"min": null,
						"show": true
					  }
					],
					"yaxis": {
					  "align": false,
					  "alignLevel": null
					}
				  }
				],
				"title": "Load、Mem",
				"type": "row"
			  }
			],
			"refresh": false,
			"schemaVersion": 16,
			"style": "dark",
			"tags": [
			  "压测平台",
			  "测试模版"
			],
			"templating": {
			  "list": []
			},
			"time": {
			  "from": "now/d",
			  "to": "now/d"
			},
			"timepicker": {
			  "refresh_intervals": [
				"5s",
				"10s",
				"30s",
				"1m",
				"5m",
				"15m",
				"30m",
				"1h",
				"2h",
				"1d"
			  ],
			  "time_options": [
				"5m",
				"15m",
				"1h",
				"6h",
				"12h",
				"24h",
				"2d",
				"7d",
				"30d"
			  ]
			},
			"timezone": "browser",
			"title": "google.local",
			"uid": "",
			"version": 0
		  },
		"folderId": 0,
		"overwrite": false
	}`
